﻿#include <QKeyEvent>
#include <QPalette>
#include <QTimer>
#include <QTextCodec>
#include <QPainter>
#include <QSettings>
#include <QString>
#include <QFile>
#include <qthread.h>
#include <QtCore/QCoreApplication>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //中文支持
    QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale( ));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale( ));

    my_tb5Enable = false;

    my_helpWin = new helpWin();
    connect(my_helpWin,SIGNAL(KeyPress()),this,SLOT(ButtonBeep()) );
    my_helpWin->hide();


    my_messageBox = new  helpWin();
    my_helpWin->hide();


    this->dataProvider = new DataProvider();
    //u盘自动升级--lbs-2017-7-15
    my_autoUpdata = new autoUpdata();
    my_autoUpdata->setGeometry(80,80,500,320);
    my_sysWatcher = new QFileSystemWatcher();
    my_sysWatcher->addPath("/mnt");
    connect( my_sysWatcher,SIGNAL(directoryChanged(QString)),this,SLOT(directoryUpdated(QString)));

    //this->mypwrctrl    = new MyPwrCtrl(this->dataProvider);

    this->dataProvider->start();
    //this->mypwrctrl->start();
    this->my_current_language = UI_ZH;
    my_displayCpuTemp = false;
    my_MeltDoneenable = false;
    my_alarmCount = 0;
    my_displayAlarm = false;
    my_objectBGTran = false;
    my_StandbyEnable = true;
    my_openGZKeysEnable = false;
    my_tempCount = 0;
    my_StandbyCount = 0;
    my_TurnOffCount = 0;

#ifndef DESK_TOP_RUN   // 背景填充，窗口框，和窗口的弹性设置，这里在机器里实现，桌面不需要  lbs-2017/4/26
    setAutoFillBackground(false);
    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground,true);
#endif

    frontWidgetTop = new MyDisplayWidget(this);
    frontWidgetBottom = new MyDisplayWidget(this);
    frontWidgetTop->setDisplayMode(DisplayBar);
    frontWidgetBottom->setDisplayMode(DisplayBar);
    frontWidgetTop->setGeometry(0,0,width(),50);
    frontWidgetBottom->setGeometry(0,height()-52,width(),52);

    //自定义控件
    bgWidget = new MyDisplayWidget(this);
    bgWidget->setDataProvider(this->dataProvider);
    bgWidget->setGeometry(0,50,width(),height()-102);
    bgWidget->repaint();
    bgWidget->setVisible(true);

    ui->setupUi(this);
    ui->stateBar->updateDateTime(dataProvider->getCurrentTime());
    //ui->stateBar->updateDateTime(ui->dateTimeEdit->dateTime());
    ui->stateBar->show();
    //此处为测试用暂时赋值

#ifndef DESK_TOP_RUN
    //设置全屏
    this->showFullScreen();
#endif

    //注册本窗口监听的eventFilter
    ui->dateTimeEdit->installEventFilter(this);
    ui->tb4Sub4S6->installEventFilter(this);
    this->installEventFilter(this);
    this->initButtons();
    this->enterStateMode();
    this->setFocus();

    //连接刷新显示槽和定时信号
    QTimer* refreshTimer = new QTimer();
    connect(refreshTimer,SIGNAL(timeout()),this,SLOT(refreshStatBar()));
    refreshTimer->start(1000);

    QTimer* flashTimer = new QTimer(this);
    connect(flashTimer,SIGNAL(timeout()),this,SLOT(flashHeatingIcon()));
    flashTimer->start(1000);

    this->dataProvider->OV5640_AEC(0);
    this->dataProvider->OV5640_AGC(0);

    refreshBright = new QTimer(this);
    connect(refreshBright,SIGNAL(timeout()),this,SLOT(refreshAGCSet()));
    refreshBright->start(500); // 0.5s

    connect(dataProvider,SIGNAL(updateMeltState()),this,SLOT(refreshUI()));
    connect(dataProvider,SIGNAL(updateMeltState()),dataProvider,SLOT(FusionActions()));
    connect(dataProvider,SIGNAL(updateDynamicUI(int)),this,SLOT(refreshDynamicUI(int)));
    connect( dataProvider,SIGNAL(buttonPress()), this, SLOT(BTPressEvent()));

     connect(dataProvider,SIGNAL(reboot()),this,SLOT(sysreboot()));
     connect(dataProvider,SIGNAL(powerOff(int)),this,SLOT(powerOff(int )));
     connect( dataProvider ,SIGNAL(copyProg(QString)), my_autoUpdata,SLOT(progress(QString)));
     connect(my_autoUpdata ,SIGNAL(copyFile()), dataProvider, SLOT(copyUDisk()));

    this->RecordIdx = 0;
    this->HeatCntr  = 3;
    my_saveSettingCount = 0;
    this->flashAlt  = true;
    this->dataProvider->SetFusionReg(rBEEP_TONE,113600);     //440hz beep
    this->dataProvider->SetFusionReg(rBEEP_ON  ,4*1000*10000);
    this->dataProvider->setMeltState(Idle);
    this->initSelectedButtons();
    this->refreshSpeedMode(0);

    //lbs 2017/7/15
    my_dischargeCount = this->dataProvider->getIniItemVal("DischargeNum"); //discharge count;
    int dischargeTime = this->dataProvider->getIniItemVal("DischargingTime");

    if( my_dischargeCount > dischargeTime)
    {
          my_messageBox->setMessage("警告",tr("建议更换电极针！"));
    }

    //init backLight
    if( !this->dataProvider->HaskeyExis("BackLight") )
    {
       this->dataProvider->setIniItem("BackLight",100);
    }
    this->dataProvider->SetFusionReg(rBACKLIGHT, this->dataProvider->getIniItemVal("BackLight"));



    //数据初始化
    this->dataProvider->SetPwrOff(this->dataProvider->getIniItemVal("TurnOff"));

    if( !this->dataProvider->HaskeyExis("TurnOff") )
    {
       this->dataProvider->setIniItem("TurnOff",60);
    }

    if( !this->dataProvider->HaskeyExis("StandBy") )
    {
       this->dataProvider->setIniItem("StandBy",60);
    }



//    this->dataProvider->SetFusionReg(rCAM0_BUF0,0x20200000);
//    this->dataProvider->SetFusionReg(rCAM1_BUF0,0x20400000);


    this->dataProvider->SetFusionReg(rVGA_CTRL,7);


    //截图功能屏蔽
    //QProcess::execute("/mnt/mmcblk0p1/my_screen.sh");

    //修改权限
    this->setPermissions(true);



    my_batCount = 0;


}
MainWindow::~MainWindow()
{
    delete ui;
    delete this->bgWidget;
    delete this->dataProvider;
}

void MainWindow::directoryUpdated(const QString &path)
{
       const QDir dir(path);
       QStringList newEntryList = dir.entryList(QDir::NoDotAndDotDot  | QDir::AllDirs | QDir::Files, QDir::DirsFirst);

        if( newEntryList.contains("sda1"))
        {
            my_autoUpdata->show();
            my_autoUpdata->setUpdatePath( path+ "/sda1");
        }else{
            my_autoUpdata->rebootSys();
        }
}


//加热结束
void MainWindow::flashHeatingIcon()
{
    if(!ifStateMode) return;

    if((this->dataProvider->GetFusionReg(rHEAT_CTRL)&0x80000000) != 0)
    {
        if( this->hasFocus() )
        {

        this->flashAlt ^= true;
            if(this->flashAlt == true)
            {
                if( my_objectBGTran )
                {
                   ui->tb5->setStyleSheet("");
                }else{
                   ui->tb5->setStyleSheet("border-image:url(:/new/prefix1/heating_light.png)");
                }

            }
            else
            {                
                if( my_objectBGTran )
                {
                   ui->tb5->setStyleSheet("");
                }else{
                   ui->tb5->setStyleSheet("border-image:url(:/new/prefix1/heating_dark.png)");
                }
            }
        }

        HeatCntr = 0;
    }
    else{

        if(HeatCntr < 5) HeatCntr ++;
        if(HeatCntr == 1){
            int tone[5]={3,2,1,0,0};
            this->dataProvider->BeepTone(tone,3);
        }
        if( this->hasFocus() )
        {
            if( my_objectBGTran )
            {
               ui->tb5->setStyleSheet("");
            }else{
               ui->tb5->setStyleSheet("border-image:url(:/new/prefix1/heating_dark.png)");
            }
        }
    }
}

void MainWindow::initSelectedButtons()
{
    ui->tb1Sub1->setupSelectedButton();
    ui->tb1Sub2->setupSelectedButton();
    ui->tb1Sub3->setupSelectedButton();
    /*
    ui->tb1Sub4->setupSelectedButton();
    ui->tb1Sub5->setupSelectedButton();
    ui->tb1Sub6->setupSelectedButton();
    */

    ui->tb2Sub1->setupSelectedButton();
    ui->tb2Sub2->setupSelectedButton();

    ui->tb3Sub1->setupSelectedButton();
    ui->tb3Sub2->setupSelectedButton();
}

void MainWindow::refreshUI()
{
    MeltState  ms;
    int        caliberation_info[5];
    unsigned int suggest[2];

    ms = this->dataProvider->getMeltState();
    if(!ifStateMode) return; //state mode don't refresh

    //当前溶接步骤；
    if(this->hasFocus())
    {
        QString tmp1=dataProvider->getMeltStateStr();
        ui->tb4->setText(QString("%1").arg(tmp1));

        QString tmp2=dataProvider->getSpeedModeStr();
        ui->tb3->setText(QString("%1").arg(tmp2));
    }


    //熔接结束
    switch( ms ){
        case Idle:
            this->bgWidget->setDisplayMode(DisplayXY);
            break;        
        case SysCheck:
            break;
        case MeltDebug:
            this->bgWidget->getAuxPos();
            this->bgWidget->SetAuxPosString();
            this->bgWidget->setDisplayMode(DisplayDebug);
            this->dataProvider->OV5640_XOFFSET(1,0);
            break;
        case MeltDone:
            this->dataProvider->AddDbSpliceRecord();
            this->my_helpWin->setMessageList( this->dataProvider->getJoint_process());
            break;
        case MeltAgain:
            break;
        case TestSave:
            this->dataProvider->cliberate_process();
            this->dataProvider->getCaliberationInfo(caliberation_info);
            this->dataProvider->applyDischargingSuggestion(suggest); //refresh confige param
            if((caliberation_info[4] & 0xf) == 0){
                this->dataProvider->setSpeedMode(Precise);
                this->refreshSpeedMode(0);

                this->bgWidget->setCaliberateInfo();
                this->bgWidget->setDisplayMode(DisplayDiscargeDone);             
            }
            else {
                this->bgWidget->setCaliberateInfo();
                this->bgWidget->setDisplayMode(DisplayDiscargeDone);
                this->dataProvider->SetPushKey();
                //this->dataProvider->SetFusionReg(rPUSH_CTRL,(this->dataProvider->GetFusionReg(rPUSH_CTRL)&0xffffff00)|0x101|(0x1 << 10));//set caliberation_mod
            }
            break;
        case MeltInit: //开盖后状态

            if(false){
                this->bgWidget->setRemindMsg(getLanguageTr(tr_MS_replace)); //已放电12345次\n请更换电极
                this->bgWidget->setDisplayMode(DisplayRemind);
            }
            else
            {
                this->bgWidget->setRemindMsg(getLanguageTr(tr_MS_Init));//已放电12345次\n请更换电极
                this->bgWidget->setDisplayMode(DisplayRemind);
            }
            my_openGZKeysEnable = false;
            my_displayCpuTemp = false;
            break;
        case MeltCamErr:
            this->bgWidget->setRemindMsg("系统自检失败，重启中，请稍候...");
            this->bgWidget->setDisplayMode(DisplayRemind);
            my_displayCpuTemp = false;
            break;
        case MeltFiberError:
            if (((this->dataProvider->GetFusionReg(rFIBER_STATUS)&0x00000003) != 0x0)
              &&((this->dataProvider->GetFusionReg(rFIBER_STATUS)&0x00000030) != 0x0))
            {
                this->bgWidget->setRemindMsg(getLanguageTr(tr_MS_twoFiber)); //请重放两光纤
            }
            else if (((this->dataProvider->GetFusionReg(rFIBER_STATUS)&0x00000030) != 0x0))
            {
                this->bgWidget->setRemindMsg(getLanguageTr(tr_MS_rightFiber));//请重放右光纤
            }
            else if (((this->dataProvider->GetFusionReg(rFIBER_STATUS)&0x00000003) != 0x0))
            {
                this->bgWidget->setRemindMsg(getLanguageTr(tr_MS_leftFiber));//请重放左光纤
            }
            else if(this->dataProvider->getFaceResult() == 1)
            {
                this->bgWidget->setRemindMsg(getLanguageTr(tr_MS_leftBad));//左端面不良
            }
            else if(this->dataProvider->getFaceResult() == 2)
            {
                this->bgWidget->setRemindMsg(getLanguageTr(tr_MS_rightBad));//右端面不良
            }
            else
            {
                if( dataProvider->hasFace() )
                {
                    this->bgWidget->setRemindMsg(getLanguageTr(tr_MS_twoFiber));//请重放两光纤
                }else{
                    this->bgWidget->setRemindMsg(getLanguageTr(tr_MS_bothBad));//两端面不良
                }

            }

            this->bgWidget->setDisplayMode(DisplayRemind);
            this->my_helpWin->setMessageList( this->dataProvider->getJoint_process());
            break;
        case MeltPwrSave:
            this->bgWidget->setRemindMsg(getLanguageTr(tr_MS_power)+QString("......"));//正在进入省电模式......
            this->bgWidget->setDisplayMode(DisplayRemind);
            break;
        case MeltPwrDown:
            this->bgWidget->setRemindMsg(getLanguageTr(tr_MS_closeSys)+QString("......"));//正在关闭系统......
            this->bgWidget->setDisplayMode(DisplayRemind);
            break;
        case MeltAlignDetial:
            this->bgWidget->setDisplayMode(DisplayDetail);
            break;
        case MeltBaseErr:
            this->bgWidget->setRemindMsg("对芯区域异常、请检查光纤位置或:\n1.镜头清洁度 2.防风罩是否扣合");
            this->bgWidget->setDisplayMode(DisplayRemind);
            break;
        case MeltTention:
            //this->bgWidget->setRemindMsg(melt_Tention + QString("......"));
            this->bgWidget->setRemindMsg("张力测试完毕");
            this->bgWidget->setDisplayMode(DisplayRemind);
            break;
        case MeltRecalib:
            qDebug("Maybe caliberation!");
            this->dataProvider->InitFusionCfgs();
            this->dataProvider->SetPushKey();
            break;
        default:
            this->bgWidget->setDisplayMode(DisplayXY);
            break;
    }

    this->bgWidget->repaint();

}

void MainWindow::refreshAGCSet()
{
    unsigned int m3,m4;
    volatile int x1,x2;
    unsigned int ValTemp;
    int brightDisable = dataProvider->getIniItemVal("BrightDisable");
    if( brightDisable == 0 )
    {
        // adjust when idle AND shell closed
        if(this->hasFocus()&&(dataProvider->getMeltState() == Idle)&&((dataProvider->GetFusionReg(rTOUCH_STATUS)&0x08) == 0x0))
        {
            this->dataProvider->Fiber_bright(&m4,&m3);
            if(((abs(160 - m3) > 5 )&&(abs(160 - m3) < 100 ))||
               ((abs(160 - m4) > 5 )&&(abs(160 - m4) < 100 ))) {
                this->dataProvider->OV5640_AEC(0);
                x1 = (int)(160.0 - m3*1.0)/5.0;
                x2 = (int)(160.0 - m4*1.0)/5.0;
                qDebug("Try adjust bright m3=%d m4=%d x1=%d x2=%d",m3,m4,x1,x2);

                AdjustCmos("AGC0", x1);
                AdjustCmos("AGC1", x2);

                this->dataProvider->OV5640_AGC(0);
            }
        }
    }




    if( !(dataProvider->GetFusionReg(rTOUCH_STATUS)&0x08) == 0x0)
    {
        //qDebug() <<"open GZ" <<  dataProvider->getMeltState() <<__FILE__<<__LINE__;
        //开盖
        my_StandbyCount = 0;
        my_StandbyEnable = true;
        my_closGZEnable = false;
        my_TurnOffCount = 0;

    }else{
        //关盖
        //qDebug() <<"close GZ" <<  dataProvider->getMeltState() <<__FILE__<<__LINE__;
        if( !my_closGZEnable)
        {
            if(this->dataProvider->getMeltMode() != Manual)
            {
                ValTemp = this->dataProvider->GetFusionReg(rPUSH_CTRL);
                this->dataProvider->SetFusionReg(rPUSH_CTRL,ValTemp | (0x1 << 10)); // enable fusion func

                this->dataProvider->SetPushKey();
                //this->dataProvider->SetFusionReg(rKEY_VALUE,0xdff);
                //this->dataProvider->SetFusionReg(rKEY_VALUE,0xFFF);
            }

             my_closGZEnable =true;

        }
    }
}

void MainWindow::refreshStatBar()
{
    int bGrade;
    qreal x1;
    qreal cpu_temp;

    if(this->hasFocus()&&(dataProvider->getMeltState() == Idle))
    {
        QString tmp1=dataProvider->getMeltStateStr();
        ui->tb4->setText(QString("%1").arg(tmp1));
    }

    //刷新电量

    if(!(this->dataProvider->GetFusionReg(rHEAT_CTRL)&0x80000000) != 0)
    {
        if( my_batCount >= 3 )
        {
            ui->stateBar->updateElec(dataProvider->getBatGrade());
            my_batCount = 0;
        }else{
           my_batCount++;
        }

    }


    //刷新时间
    ui->stateBar->updateDateTime(dataProvider->getCurrentTime());
    ui->stateBar->repaint();


    x1 = (this->dataProvider->GetFusionReg(rCORETEMP)*503.975 )/0x10000 - 273.15;

    this->bgWidget->ShowAuxPos();
//    this->dataProvider->SetFusionReg(rVGA_CTRL,7);


    //lbs 2017/7/20
    if(x1 < 0) {
        cpu_temp = 25.5;
    }
    else cpu_temp = x1;

    if(cpu_temp > 66.0)
    {
        ui->label_cpuTemp->setStyleSheet("color: rgb(255, 0, 0);");
    }

    my_alarmCount++;

    int maxTemp = this->dataProvider->getIniItemVal("AlarmTemp");
    if( maxTemp == 0 )
    {
        maxTemp = 80;
    }

    if( cpu_temp > maxTemp )
    {
        if( my_alarmCount >= 30 ) //30s check
        {
            alarmSlot();
            this->dataProvider->setIniItem("lastTemp",cpu_temp);
            my_alarmCount = 0;
        }

    }else if( my_displayAlarm )
    {
        if( this->hasFocus() && this->dataProvider->getMeltState() != MeltDebug)
            {

                this->dataProvider->setDisplayXY();
                this->bgWidget->setDisplayMode(DisplayXY);
                this->bgWidget->repaint();
                this->setFocus();
            }
        my_displayAlarm = false;
    }

//   //刷新CPU 温度
    if( my_displayCpuTemp )
    {
       ui->label_cpuTemp->setText(QString().sprintf("%.1f",cpu_temp) + "°C");
    }else{
        ui->label_cpuTemp->setText("");
    }


    if( this->hasFocus() && (this->dataProvider->getMeltState() != MeltDebug)) // 当该页面不在设置界面也不再工程页面时
    {
        if( my_tempCount == 0 )//互斥操作，只操作一次
        {
            my_displayCpuTemp = false;
            my_objectBGTran = false;
            this->frontWidgetTop->setBGTransparent(false);
            this->frontWidgetBottom->setBGTransparent(false);
            this->frontWidgetTop->repaint();
            this->frontWidgetBottom->repaint();
            ui->tb5->setStyleSheet("border-image:url(:/new/prefix1/heating_dark.png)");
            this->dataProvider->setMeltState(MeltInit);
            //this->dataProvider->setMeltState(Idle);
        }
            my_tempCount = 1;

    }else{
        my_tempCount = 0;
    }


     //待机
    my_StandbyCount ++;
    //qDebug() <<my_StandbyCount << this->dataProvider->getIniItemVal("StandBy")  <<__FILE__<<__LINE__;
     if( this->dataProvider->getIniItemVal("StandBy") == 0 )
     {
         if( my_StandbyCount >= 20)
         {
            this->dataProvider->SetFusionReg(rBACKLIGHT, 0);
             my_StandbyCount = 0;
             my_StandbyEnable = false;
         }
     }else if( my_StandbyCount >= this->dataProvider->getIniItemVal("StandBy")*60)
     {
         this->dataProvider->SetFusionReg(rBACKLIGHT, 0);
         my_StandbyCount = 0;
         my_StandbyEnable = false;
     }

     if( my_StandbyEnable )
     {
        this->dataProvider->SetFusionReg(rBACKLIGHT, this->dataProvider->getIniItemVal("BackLight"));
     }
    // qDebug() << my_StandbyCount << my_StandbyEnable << this->dataProvider->getIniItemVal("StandBy") <<__FILE__<<__LINE__;

    my_TurnOffCount ++;
    //qDebug() << my_TurnOffCount << this->dataProvider->GetPwrOff() <<__FILE__<<__LINE__;
    if(my_TurnOffCount >= this->dataProvider->GetPwrOff())
    {
        this->dataProvider->pwroff();
    }

}

void MainWindow::refreshRecord(int id)
{
    this->dataProvider->setRecords(0);

    ui->tb5Sub1S1->setText(this->dataProvider->getRecords(false).at(0));
    ui->tb5Sub1S2->setText(this->dataProvider->getRecords(false).at(1));
    ui->tb5Sub1S3->setText(this->dataProvider->getRecords(false).at(2));
    ui->tb5Sub1S4->setText(this->dataProvider->getRecords(false).at(3));
    ui->tb5Sub1S5->setText(this->dataProvider->getRecords(false).at(4));
    ui->tb5Sub1S6->setText(this->dataProvider->getRecords(false).at(5));
    ui->tb5Sub1S7->setText(this->dataProvider->getRecords(false).at(6));
    ui->tb5Sub1S8->setText(this->dataProvider->getRecords(false).at(7));

    qDebug("refreshRecord done!!");
}

void MainWindow::upRefreshRecord(int id)
{
    RecordIdx--;
    if(RecordIdx < 0) RecordIdx = 0;
    this->dataProvider->setRecords(RecordIdx);

    ui->tb5Sub1S1->setText(this->dataProvider->getRecords(false).at(0));
    ui->tb5Sub1S2->setText(this->dataProvider->getRecords(false).at(1));
    ui->tb5Sub1S3->setText(this->dataProvider->getRecords(false).at(2));
    ui->tb5Sub1S4->setText(this->dataProvider->getRecords(false).at(3));
    ui->tb5Sub1S5->setText(this->dataProvider->getRecords(false).at(4));
    ui->tb5Sub1S6->setText(this->dataProvider->getRecords(false).at(5));
    ui->tb5Sub1S7->setText(this->dataProvider->getRecords(false).at(6));
    ui->tb5Sub1S8->setText(this->dataProvider->getRecords(false).at(7));

    qDebug("Up refreshRecord done!!");
}

void MainWindow::downRefreshRecord(int id)
{
    RecordIdx++;
    this->dataProvider->setRecords(RecordIdx);

    ui->tb5Sub1S1->setText(this->dataProvider->getRecords(false).at(0));
    ui->tb5Sub1S2->setText(this->dataProvider->getRecords(false).at(1));
    ui->tb5Sub1S3->setText(this->dataProvider->getRecords(false).at(2));
    ui->tb5Sub1S4->setText(this->dataProvider->getRecords(false).at(3));
    ui->tb5Sub1S5->setText(this->dataProvider->getRecords(false).at(4));
    ui->tb5Sub1S6->setText(this->dataProvider->getRecords(false).at(5));
    ui->tb5Sub1S7->setText(this->dataProvider->getRecords(false).at(6));
    ui->tb5Sub1S8->setText(this->dataProvider->getRecords(false).at(7));

    qDebug("Down refreshRecord done!!");
}

void MainWindow::refreshStatistic(int /*id*/)
{
    QList <int >  results;
    results = this->dataProvider->getWeldResult();

   // ValTemp = this->dataProvider->getIniItemVal("SuccessNum");

    QString strTemp = getLanguageTr(tr_MS_success) + ":" + QString().setNum(results[1]);  //lbs 2017-04-25

    ui->tb5Sub2S1->setText( strTemp );  //lbs 2017-04-25

    //ValTemp = this->dataProvider->getIniItemVal("FailNum");
    strTemp = getLanguageTr(tr_MS_failure)+ ":" + QString().setNum(results[0]);  //lbs 2017-04-25


    ui->tb5Sub2S2->setText( strTemp );  //lbs 2017-04-25

}

void MainWindow::refreshMeltMode(int id){
    int ValTemp;
    ValTemp = this->dataProvider->getIniItemVal("MeltMode");

    ui->tb1Sub1->setSelected(false);
    ui->tb1Sub2->setSelected(false);
    ui->tb1Sub3->setSelected(false);
    /*
    ui->tb1Sub4->setSelected(false);
    ui->tb1Sub5->setSelected(false);
    ui->tb1Sub6->setSelected(false);
    */

    switch( ValTemp ){
    case 0:
        ui->tb1Sub1->setSelected(true);  //自动熔接模式
        break;
    case 1:
        ui->tb1Sub2->setSelected(true); //手动熔接模式
        break;
    case 2:
        //ui->tb1Sub5->setSelected(true);//推纤暂停模式
        break;
    case 4:
        //ui->tb1Sub3->setSelected(true);//连续暂停模式
        break;
    case 3:
        ui->tb1Sub3->setSelected(true);
        //ui->tb1Sub4->setSelected(true);//对准暂停模式
        break;
    case 5:
        //ui->tb1Sub6->setSelected(true);//放电校准模式
        break;
    default:
        break;
    }
}

void MainWindow::refreshHeatMode(int id)
{
    int ValTemp;
    ValTemp = this->dataProvider->getIniItemVal("HeatingMode");

    ui->tb2Sub1->setSelected(false);
    ui->tb2Sub2->setSelected(false);

    switch( ValTemp ){
    case 0:
        ui->tb2Sub1->setSelected(true);  //自动加热模式
        break;
    case 1:
        ui->tb2Sub2->setSelected(true); //手动加热模式
        break;
    default:
        break;

    }
}

void MainWindow::refreshSpeedMode(int id)
{
    int ValTemp;
    ValTemp = this->dataProvider->getIniItemVal("SpeedMode");

    ui->tb3Sub1->setSelected(false);
    ui->tb3Sub2->setSelected(false);
    switch( ValTemp ){
    case 0:
        ui->tb3Sub1->setSelected(true);  //极速模式
        break;
    case 1:
        ui->tb3Sub2->setSelected(true);  //精准模式
        break;
    default:
        break;
    }

}

//需特别响应的UI事件
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if(obj==ui->tb4Sub4S6&&event->type()==QEvent::KeyPress)
    {
        QKeyEvent* ke=(QKeyEvent*)event;
        if(ke->key()==Qt::Key_Q)
        {
            ui->dateTimeEdit->setDisplayFormat("yyyy-MM-dd HH:mm:ss");
            ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
            ui->dateTimeEdit->setVisible(true);
            ui->dateTimeEdit->setFocus();
        }
    }
    else if(obj==ui->dateTimeEdit)
    {
        if(event->type()==QEvent::KeyPress)
        {
            QKeyEvent* ke = (QKeyEvent*)event;
            if(ke->key()==Qt::Key_Q)
            {
                dataProvider->setCurrentTime(ui->dateTimeEdit->dateTime());
                ui->tb4Sub4S6->setFocus();
                ui->dateTimeEdit->setVisible(false);
                return true;
            }
            else if((ke->key()==Qt::Key_R)||(ke->key()==Qt::Key_F6))
            {
                //Cancel and back
                ui->tb4Sub4S6->setFocus();
                ui->dateTimeEdit->setVisible(false);
                return true;
            }
            else return false;
        }
        return false;
    }

    return QWidget::eventFilter(obj, event);
}

void MainWindow::enterEditMode()
{
    unsigned int ValTemp;

    this->ifStateMode=false;

    ValTemp = this->dataProvider->GetFusionReg(rPUSH_CTRL); // disable fusion func
    this->dataProvider->SetFusionReg(rPUSH_CTRL,ValTemp & (~(0x1 << 10)));

    ui->tb1->setText( getLanguageTr(tr_enum_weld) );    //熔接模式设置
    ui->tb2->setText( getLanguageTr(tr_enum_Heating) ); // 加热模式设置
    ui->tb3->setText( getLanguageTr(tr_enum_speed) );   //速度模式设置
    ui->tb4->setText( getLanguageTr(tr_enum_system) );  //整机设置
    ui->tb5->setText( getLanguageTr(tr_enum_record) );  //熔接记录

    ui->tb1->setStyleNormal();
    ui->tb2->setStyleNormal();
    ui->tb3->setStyleNormal();
    ui->tb4->setStyleNormal();
    ui->tb5->setStyleNormal();

}

void MainWindow::enterStateMode()
{
    unsigned int ValTemp;

    this->ifStateMode=true;

    ValTemp = this->dataProvider->GetFusionReg(rPUSH_CTRL);
    this->dataProvider->SetFusionReg(rPUSH_CTRL,ValTemp | (0x1 << 10)); // enable fusion func

    QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale( ));////显示中文！
    QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale( ));////显示中文！


    ui->tb1->setText(dataProvider->getMeltModeStr());//get the var from fpga
    ui->tb2->setText(dataProvider->getHeatingModeStr());
    ui->tb3->setText(dataProvider->getSpeedModeStr());

    //qDebug("init tb1 setting %s",qPrintable(dataProvider->getMeltModeStr()));
    ui->tb5->setText("");

    ui->tb1->setStyleSheet("border-image:none;");
    ui->tb2->setStyleSheet("border-image:none;");
    ui->tb3->setStyleSheet("border-image:none;");
    ui->tb4->setStyleSheet("border-image:none;");
    ui->tb5->setStyleSheet("border-image:url(:/new/prefix1/heating_dark.png)");
}


void MainWindow::keyPressEvent(QKeyEvent *e)
{

    unsigned int suggest[2];
    int ValTemp;
    MeltState ms;

    unsigned int nS1,nS2;
    unsigned int Timestamp[6];
    qreal    x1,x2;
    nS1=nS2=0;

    QKeyEvent* ke = (QKeyEvent*)e;
    this->dataProvider->SetFusionReg(rBEEP_ON  ,300*10000);  //0.3s

    ms = this->dataProvider->getMeltState();
    //ui->label_cpuTemp->hide();
    if ( ms != MeltDebug )
    {
        my_displayCpuTemp = false;
    }
    my_StandbyEnable = true;
    my_StandbyCount = 0;
    my_TurnOffCount= 0;

#ifdef DESK_TOP_RUN
    if(ke->key()==Qt::Key_A){
        if (ms >= MeltPwrDown ) ms = Idle;
        this->dataProvider->setMeltState(MeltState (ms + 1));
        //this->dataProvider->StateChange();
        qDebug("key read:%x %d",ke->key(),(int) ms);
    }
#endif


if ((e->modifiers() == Qt::ShiftModifier) && (e->key() == Qt::Key_Up))
    {
        if(this->dataProvider->getMeltMode() == Manual){
            this->dataProvider->x_motor_ctrl(1,(10 - this->dataProvider->getManualSpeed()),0,0,0,0xffff);
         }
            this->dataProvider->get_rtc_remote_time(Timestamp);
            qDebug("rTOUCH_STATUS = %x :rPUSH_STAT = %x :rFIBER_STATUS = %x :rPUSH_MOTOR= %x, MeltState  = %x",
            this->dataProvider->GetFusionReg(rTOUCH_STATUS),
            this->dataProvider->GetFusionReg(rPUSH_STAT),
            this->dataProvider->GetFusionReg(rFIBER_STATUS),
            this->dataProvider->GetFusionReg(rPUSH_MOTOR),
            this->dataProvider->getMeltState());
            qDebug("rHEAT_CTRL = %x : rPUSH_CTRL    = %x : rAUX_CTRL    = %x",
            this->dataProvider->GetFusionReg(rHEAT_CTRL),
            this->dataProvider->GetFusionReg(rPUSH_CTRL),
            this->dataProvider->GetFusionReg(rAUX_CTRL));
            qDebug("CAM0Param=%x,CAM1Param=%x",
            this->dataProvider->GetFusionReg(rCAM0_PARAM),
            this->dataProvider->GetFusionReg(rCAM1_PARAM));
            x1 = (this->dataProvider->GetFusionReg(rCORETEMP)*503.975 )/0x10000 - 273.15;
            x2 = (this->dataProvider->GetFusionReg(rVPIN) * 13.0)/0x10000;
            qDebug("CPU TEMP=%.2f deg,rVPIN=%.2f vol",x1,x2);

        }else if(ke->key()==Qt::Key_Up)
        {
            if(ms == MeltDebug){
                if(this->bgWidget->getAdjMode() == 8){
                    AdjustCmos("AGC0",1);
                    this->dataProvider->OV5640_AGC(0);
                }
                else if(this->bgWidget->getAdjMode() == 0){
                    this->bgWidget->setHiddenFunc(10);
                    t_recordClean += "3";
                }
            }else{
                this->dataProvider->setDisplayX();
                this->bgWidget->setDisplayMode(DisplayX);
                this->bgWidget->repaint();
            }
        }


if ((e->modifiers() == Qt::ShiftModifier) && (e->key() == Qt::Key_Q))
{
    this->dataProvider->setMeltState(MeltDebug);
    my_objectBGTran = true;
    ui->tb5->setStyleSheet("");
    this->frontWidgetTop->setBGTransparent(true);
    this->frontWidgetBottom->setBGTransparent(true);
    this->frontWidgetTop->repaint();
    this->frontWidgetBottom->repaint();

    this->bgWidget->getAuxPos();
    this->bgWidget->SetAuxPosString();
    this->bgWidget->setDBKey(DB_NONE);   //lbs
    t_recordClean.clear();
    my_displayCpuTemp = true;

}else if(ke->key()==Qt::Key_Q){

//        this->dataProvider->setDisplayXY();
//        this->bgWidget->setDisplayMode(DisplayXY);
//        this->bgWidget->repaint();
//        this->setFocus();
//        QString tmp1=dataProvider->getMeltStateStr();
//        ui->tb4->setText(QString("%1").arg(tmp1));
//        my_displayCpuTemp = false;
//        my_objectBGTran = false;
//        this->frontWidgetTop->setBGTransparent(false);
//        this->frontWidgetBottom->setBGTransparent(false);
//        this->frontWidgetTop->repaint();
//        this->frontWidgetBottom->repaint();
//        ui->tb5->setStyleSheet("border-image:url(:/new/prefix1/heating_dark.png)");
//        this->dataProvider->setMeltState(MeltInit);



        //放电结束
        if( ms != MeltDebug )
        {
            if(ms == TestSave)
            {
                this->dataProvider->applyDischargingSuggestion(suggest); //refresh confige param
                if(((this->dataProvider->getIniItemVal("ParamSel"))&0x1) == 0){
                    ui->tb4Sub2S1S2->setCurVal(suggest[0]); //refresh UI
                    ui->tb4Sub2S1S4->setCurVal(suggest[1]); //refresh UI
                }
                else
                {
                    ui->tb4Sub2S2S2->setCurVal(suggest[0]); //refresh UI
                    ui->tb4Sub2S2S4->setCurVal(suggest[1]); //refresh UI
                }
                qDebug("TestSave paramsel=%d,s0=%d,s1=%d",((this->dataProvider->getIniItemVal("ParamSel"))&0x1),suggest[0],suggest[1]);

                this->dataProvider->setMeltState(MeltInit);
                this->dataProvider->setDisplayXY();
                this->bgWidget->setDisplayMode(DisplayXY);
                this->bgWidget->repaint();
            }else if(ms == MeltDone)
                {
                    this->bgWidget->setDisplayMode(DisplayXY);
                    this->bgWidget->repaint();
                }
//                else {
//                    if( !my_openGZKeysEnable )
//                    {
//                        this->dataProvider->setDisplayXY();
//                        ui->tb1->setFocus();
//                    }
//                }
//        }else{
//            my_openGZKeysEnable = true;
        }

}else if(ke->key()==Qt::Key_M){  //推进
    qDebug("rPUSH_CTRL:: %x",this->dataProvider->GetFusionReg(rPUSH_CTRL));
    qDebug("rKEY_VALUE:: %x",this->dataProvider->GetFusionReg(rKEY_VALUE));
    this->dataProvider->SetFusionReg(rKEY_VALUE,0xdff);
}


if ((e->modifiers() == Qt::ShiftModifier) && (e->key() == Qt::Key_R))
{
    this->dataProvider->x_motor_ctrl(1,1,1,1,1,0x0);
    this->dataProvider->y_motor_ctrl(1,1,1,1,1,0x0);
}else  if(ke->key()==Qt::Key_R){


//    if( this->hasFocus() )
//    {
////        qDebug() << "open cam0 cam1" <<__FILE__<<__LINE__;
////        ValTemp = (0x80<<24|0x80 << 16);
////        this->dataProvider->SetFusionReg(rCAM0_CTRL,ValTemp);
////        this->dataProvider->SetFusionReg(rCAM1_CTRL,ValTemp);
//    }else{

//    }

//    //放电结束
//    if( ms != MeltDebug )
//    {
//        qDebug() << "MeltDebug" <<__FILE__<<__LINE__;
//        if(ms == TestSave)
//        {
//            qDebug() << "TestSave" <<__FILE__<<__LINE__;
//            this->dataProvider->applyDischargingSuggestion(suggest); //refresh confige param
//            if(((this->dataProvider->getIniItemVal("ParamSel"))&0x1) == 0){
//                ui->tb4Sub2S1S2->setCurVal(suggest[0]); //refresh UI
//                ui->tb4Sub2S1S4->setCurVal(suggest[1]); //refresh UI
//            }
//            else
//            {
//                ui->tb4Sub2S2S2->setCurVal(suggest[0]); //refresh UI
//                ui->tb4Sub2S2S4->setCurVal(suggest[1]); //refresh UI
//            }
//            qDebug("TestSave paramsel=%d,s0=%d,s1=%d",((this->dataProvider->getIniItemVal("ParamSel"))&0x1),suggest[0],suggest[1]);

//            this->dataProvider->setMeltState(MeltInit);
//            this->dataProvider->setDisplayXY();
//            this->bgWidget->setDisplayMode(DisplayXY);
//            this->bgWidget->repaint();
//        }else if(ms == MeltDone)
//            {
//            qDebug() << "MeltDone" <<__FILE__<<__LINE__;
//                this->bgWidget->setDisplayMode(DisplayXY);
//                this->bgWidget->repaint();
//            }
//            else {
//                if( !my_openGZKeysEnable )
//                {
//                    this->dataProvider->setDisplayXY();
//                    ui->tb1->setFocus();
//                }

//            }
//    }else{
//        my_openGZKeysEnable = true;
//    }


    this->dataProvider->setDisplayXY();
    this->bgWidget->setDisplayMode(DisplayXY);
    this->bgWidget->repaint();
    this->setFocus();
    QString tmp1=dataProvider->getMeltStateStr();
    ui->tb4->setText(QString("%1").arg(tmp1));
    my_displayCpuTemp = false;
    my_objectBGTran = false;
    this->frontWidgetTop->setBGTransparent(false);
    this->frontWidgetBottom->setBGTransparent(false);
    this->frontWidgetTop->repaint();
    this->frontWidgetBottom->repaint();
    ui->tb5->setStyleSheet("border-image:url(:/new/prefix1/heating_dark.png)");
    this->dataProvider->setMeltState(MeltInit);

    //放电结束
    if( ms != MeltDebug )
    {
         if(ms != MeltDone)
        {
            if( !my_openGZKeysEnable )
            {
                this->dataProvider->setDisplayXY();
                ui->tb1->setFocus();
            }
         }
    }else{
        my_openGZKeysEnable = true;
    }
}



if ((e->modifiers() == Qt::ShiftModifier) && (e->key() == Qt::Key_Left))
{
//    qDebug() << "close cam0 cam1" <<__FILE__<<__LINE__;
//     ValTemp = (0x00<<24|0x80 << 16);
//     this->dataProvider->SetFusionReg(rCAM0_CTRL,ValTemp);
//     this->dataProvider->SetFusionReg(rCAM1_CTRL,ValTemp);

   // this->dataProvider->pwroff();
}else if(ke->key()==Qt::Key_Left)
{
    if(ms == MeltDebug){
        if(this->bgWidget->getAdjMode() == 8){
            AdjustCmos("AGC1",-1);
            this->dataProvider->OV5640_AGC(0);
        }
        else if(this->bgWidget->getAdjMode() == 1){
            int y_aoffset = this->dataProvider->getIniItemVal("YBasePosition");
            if(y_aoffset > -100) y_aoffset --;
            qDebug("YBasePosition decreased to %d",y_aoffset);
            this->dataProvider->setIniItem("YBasePosition",y_aoffset);
            this->dataProvider->OV5640_XOFFSET(1,3*y_aoffset);
        }
        else if(this->bgWidget->getAdjMode() == 0){
            t_recordClean += "1";
            this->bgWidget->setDBKey(DB_LEFT);   //lbs
        }
        else {
            this->bgWidget->setMinBatVal();
        }
    }
    else{
        this->dataProvider->setDisplayXY();
        this->bgWidget->setDisplayMode(DisplayXY);
        this->bgWidget->repaint();
    }
}

if ((e->modifiers() == Qt::ShiftModifier) && (e->key() == Qt::Key_Right))
{

//    qDebug() << "open cam0 cam1" <<__FILE__<<__LINE__;
//    ValTemp = (0x80<<24|0x80 << 16);
//    this->dataProvider->SetFusionReg(rCAM0_CTRL,ValTemp);
//    this->dataProvider->SetFusionReg(rCAM1_CTRL,ValTemp);

    //  this->dataProvider->OV5640_reset(); //test

}else if(ke->key()==Qt::Key_Right){
    if(ms == MeltDebug){
        if(this->bgWidget->getAdjMode() == 8){
            AdjustCmos("AGC1",1);
            this->dataProvider->OV5640_AGC(0);
        }
        else if(this->bgWidget->getAdjMode() == 1){
            int y_doffset = this->dataProvider->getIniItemVal("YBasePosition");
            if(y_doffset < 100) y_doffset ++;
            qDebug("YBasePosition increased to %d",y_doffset);
            this->dataProvider->setIniItem("YBasePosition",y_doffset);
            this->dataProvider->OV5640_XOFFSET(1,3*y_doffset);
        }
        else if(this->bgWidget->getAdjMode() == 0){
            t_recordClean += "2";
            this->bgWidget->setDBKey(DB_RIGHT);   //lbs
        }
        else {
            this->bgWidget->setMaxBatVal();
        }
    }
    else{
        this->dataProvider->setDisplayXY();
        this->bgWidget->setDisplayMode(DisplayXY);
        this->bgWidget->repaint();
    }
}

if ((e->modifiers() == Qt::ShiftModifier) && (e->key() == Qt::Key_Down))
{
   if(this->dataProvider->getMeltMode() == Manual){
       this->dataProvider->y_motor_ctrl(1,(10 - this->dataProvider->getManualSpeed()),0,0,0,0xffff);
   }
}else if(ke->key()==Qt::Key_Down)
{
    if(ms == MeltDebug){
        if(this->bgWidget->getAdjMode() == 8){
            AdjustCmos("AGC0",-1);
            this->dataProvider->OV5640_AGC(0);
        }
        else if(this->bgWidget->getAdjMode() == 0){
            this->bgWidget->setHiddenFunc(100);
            t_recordClean += "4";
        }
    }
    else{
        this->dataProvider->setDisplayY();
        this->bgWidget->setDisplayMode(DisplayY);
        this->bgWidget->repaint();
    }
}

   if ((e->modifiers() == Qt::ShiftModifier) && (e->key() == Qt::Key_D))
   {

   }else if(ke->key()==Qt::Key_D)
    {
        if(ms == MeltDebug) {
            this->dataProvider->ManulFandian(1800,100);           
        }
        else if((ms == MeltDone)||(ms == MeltAgain)) {
            this->dataProvider->ManulFandian(1800,this->dataProvider->getIniItemVal("SingleMeltIntense"));
            this->dataProvider->setMeltState(MeltAgain);
        }
    } else if(ke->key()==Qt::Key_Shift){
        t_recordClean.clear();
        if(ms == MeltDebug)
        {
            this->bgWidget->setAdjMode();
        }

        if( ms == MeltDone || ms == MeltFiberError || ms == MeltTention )
        {
            this->my_helpWin->showmessages();
        }

        this->dataProvider->screenshotManual();

    }

    if( ke->key()==Qt::Key_P) //复位
    {


        this->dataProvider->SetFusionReg(rKEY_VALUE,0xbff);
        this->dataProvider->setMeltState(MeltInit);
        this->dataProvider->setDisplayXY();
        this->bgWidget->setDisplayMode(DisplayXY);
        this->bgWidget->repaint();
        this->setFocus();
    }

    if(  ke->key()==Qt::Key_H)
    {
        this->dataProvider->SetFusionReg(rKEY_VALUE,0xeff);
    }



    if( t_recordClean  == "112212344321")
    {
        dataProvider->cleanRecords();
        t_recordClean.clear();
    }

    if( t_recordClean  == "112334")
    {
        //提取原有信息 0 关  1开
        int linght = dataProvider->getIniItemVal("BrightDisable");
        //取反
        if( 0 == linght )
        {
            dataProvider->setIniItem("BrightDisable",1);
         }else if( 1 == linght){
            dataProvider->setIniItem("BrightDisable",0);
         }
         t_recordClean.clear();
    }


    if(t_recordClean == "1234")
    {
        dataProvider->TestProg();
    }

    e->ignore();
}


void MainWindow::keyReleaseEvent ( QKeyEvent * event )
{

    switch( event->key() )
    {
        case Qt::Key_P:
        case Qt::Key_H:
        case Qt::Key_M:
            this->dataProvider->SetFusionReg(rKEY_VALUE,0xFFF);
            break;
        default:
            break;
    }
}

void MainWindow::AdjustCmos(char *agc_aec,int dir)
{
    int ValTemp;

    ValTemp = this->dataProvider->getIniItemVal(agc_aec);
    ValTemp += dir;
    if(ValTemp > 0xff) ValTemp = 0xff;
    if(ValTemp < 1) ValTemp = 1;
    this->dataProvider->setIniItem(agc_aec,ValTemp);
    qDebug("%s set to %d",agc_aec,ValTemp);
}

void MainWindow::focusInEvent(QFocusEvent *e)
{
    this->bgWidget->setDisplayMode(DisplayXY);
    this->bgWidget->repaint();
    enterStateMode();
}

void MainWindow::focusOutEvent(QFocusEvent *e)
{
    this->bgWidget->setDisplayMode(DisplayBlock);
    this->bgWidget->repaint();

    enterEditMode();
}

void MainWindow::initButtons()
{
    //设置好没个按钮的数据源，之后所有子按钮自动添加
    ui->tb1->setDataProvider(this->dataProvider);
    ui->tb2->setDataProvider(this->dataProvider);
    ui->tb3->setDataProvider(this->dataProvider);
    ui->tb4->setDataProvider(this->dataProvider);
    ui->tb5->setDataProvider(this->dataProvider);
    //设置tb1相关按钮
    ui->tb1->addChildWg(ui->tb1Sub1);
    ui->tb1->addChildWg(ui->tb1Sub2);
    ui->tb1->addChildWg(ui->tb1Sub3);
    /*
    ui->tb1->addChildWg(ui->tb1Sub4);
    ui->tb1->addChildWg(ui->tb1Sub5);
    ui->tb1->addChildWg(ui->tb1Sub6);
    */
    ui->tb1->setRightWg(ui->tb2);
    ui->tb1->setDownWg(ui->tb4);
    ui->tb1->setLeftWg(ui->tb3);

    ui->tb1Sub1->setDownWg(ui->tb1Sub2);
    ui->tb1Sub2->setDownWg(ui->tb1Sub3);
    ui->tb1Sub3->setDownWg(ui->tb1Sub1);
    /*
    ui->tb1Sub3->setDownWg(ui->tb1Sub4);
    ui->tb1Sub4->setDownWg(ui->tb1Sub5);
    ui->tb1Sub5->setDownWg(ui->tb1Sub6);
    ui->tb1Sub6->setDownWg(ui->tb1Sub1);
    ui->tb1Sub1->setUpWg(ui->tb1Sub6);
    */
    ui->tb1Sub1->setUpWg(ui->tb1Sub3);
    ui->tb1Sub2->setUpWg(ui->tb1Sub1);
    ui->tb1Sub3->setUpWg(ui->tb1Sub2);
    /*
    ui->tb1Sub4->setUpWg(ui->tb1Sub3);
    ui->tb1Sub5->setUpWg(ui->tb1Sub4);
    ui->tb1Sub6->setUpWg(ui->tb1Sub5);
    */
    ui->tb1Sub4->hide();
    ui->tb1Sub5->hide();
    ui->tb1Sub6->hide();

    //设置tb2相关按钮
    ui->tb2->addChildWg(ui->tb2Sub1);
    ui->tb2->addChildWg(ui->tb2Sub2);
    ui->tb2Sub1->setDownWg(ui->tb2Sub2);
    ui->tb2Sub2->setUpWg(ui->tb2Sub1);
    ui->tb2->setLeftWg(ui->tb1);
    ui->tb2->setRightWg(ui->tb3);
    ui->tb2->setDownWg(ui->tb5);
    ui->tb2->setUpWg(ui->tb5);

    //设置tb3相关按钮
    ui->tb3->setLeftWg(ui->tb2);
    ui->tb3->addChildWg(ui->tb3Sub1);
    ui->tb3->addChildWg(ui->tb3Sub2);
    ui->tb3Sub1->setDownWg(ui->tb3Sub2);
    ui->tb3Sub2->setUpWg(ui->tb3Sub1);
    ui->tb3->setRightWg(ui->tb1);

    //设置tb4关联按钮
    ui->tb4->setUpWg(ui->tb1);
    ui->tb4->setRightWg(ui->tb5);
    ui->tb4->setLeftWg(ui->tb5);
    ui->tb4->setDownWg(ui->tb1);
    ui->tb4->addChildWg(ui->tb4Sub1);
    ui->tb4->addChildWg(ui->tb4Sub2);
    ui->tb4->addChildWg(ui->tb4Sub3);
    ui->tb4->addChildWg(ui->tb4Sub4);
    ui->tb4->addChildWg(ui->tb4Sub5);

    ui->tb4Sub1->setDownWg(ui->tb4Sub2);
    ui->tb4Sub2->setDownWg(ui->tb4Sub3);
    ui->tb4Sub3->setDownWg(ui->tb4Sub4);
    ui->tb4Sub4->setDownWg(ui->tb4Sub5);
    ui->tb4Sub5->setDownWg(ui->tb4Sub1);

    ui->tb4Sub5->setUpWg(ui->tb4Sub4);
    ui->tb4Sub4->setUpWg(ui->tb4Sub3);
    ui->tb4Sub3->setUpWg(ui->tb4Sub2);
    ui->tb4Sub2->setUpWg(ui->tb4Sub1);
    ui->tb4Sub1->setUpWg(ui->tb4Sub5);

    //tb4Sub1设置
    ui->tb4Sub1->addChildWg(ui->tb4Sub1S1);
    ui->tb4Sub1->addChildWg(ui->tb4Sub1S2);
    ui->tb4Sub1->addChildWg(ui->tb4Sub1S3);
    ui->tb4Sub1->addChildWg(ui->tb4Sub1S4);
    ui->tb4Sub1->addChildWg(ui->tb4Sub1S5);
    ui->tb4Sub1S1->setDownWg(ui->tb4Sub1S2);
    ui->tb4Sub1S2->setDownWg(ui->tb4Sub1S3);
    ui->tb4Sub1S3->setDownWg(ui->tb4Sub1S4);
    ui->tb4Sub1S4->setDownWg(ui->tb4Sub1S5);
    ui->tb4Sub1S5->setDownWg(ui->tb4Sub1S1);
    ui->tb4Sub1S5->setUpWg(ui->tb4Sub1S4);
    ui->tb4Sub1S4->setUpWg(ui->tb4Sub1S3);
    ui->tb4Sub1S3->setUpWg(ui->tb4Sub1S2);
    ui->tb4Sub1S2->setUpWg(ui->tb4Sub1S1);
    ui->tb4Sub1S1->setUpWg(ui->tb4Sub1S5);
    ui->tb4Sub1->showChildWgs(false);

    //tb4Sub2设置
    ui->tb4Sub2->addChildWg(ui->tb4Sub2S1);
    ui->tb4Sub2->addChildWg(ui->tb4Sub2S2);
    ui->tb4Sub2S1->setDownWg(ui->tb4Sub2S2);
    ui->tb4Sub2S2->setUpWg(ui->tb4Sub2S1);
    ui->tb4Sub2->showChildWgs(false);

    //tb4Sub2S1
    ui->tb4Sub2S1->addChildWg(ui->tb4Sub2S1S1);
    ui->tb4Sub2S1->addChildWg(ui->tb4Sub2S1S2);
    ui->tb4Sub2S1->addChildWg(ui->tb4Sub2S1S3);
    ui->tb4Sub2S1->addChildWg(ui->tb4Sub2S1S4);
    ui->tb4Sub2S1S1->setDownWg(ui->tb4Sub2S1S2);
    ui->tb4Sub2S1S2->setDownWg(ui->tb4Sub2S1S3);
    ui->tb4Sub2S1S3->setDownWg(ui->tb4Sub2S1S4);
    ui->tb4Sub2S1S4->setUpWg(ui->tb4Sub2S1S3);
    ui->tb4Sub2S1S3->setUpWg(ui->tb4Sub2S1S2);
    ui->tb4Sub2S1S2->setUpWg(ui->tb4Sub2S1S1);
    ui->tb4Sub2S1->showChildWgs(false);

    //tb4Sub2S2
    ui->tb4Sub2S2->addChildWg(ui->tb4Sub2S2S1);
    ui->tb4Sub2S2->addChildWg(ui->tb4Sub2S2S2);
    ui->tb4Sub2S2->addChildWg(ui->tb4Sub2S2S3);
    ui->tb4Sub2S2->addChildWg(ui->tb4Sub2S2S4);
    ui->tb4Sub2S2S1->setDownWg(ui->tb4Sub2S2S2);
    ui->tb4Sub2S2S2->setDownWg(ui->tb4Sub2S2S3);
    ui->tb4Sub2S2S3->setDownWg(ui->tb4Sub2S2S4);
    ui->tb4Sub2S2S4->setUpWg(ui->tb4Sub2S2S3);
    ui->tb4Sub2S2S3->setUpWg(ui->tb4Sub2S2S2);
    ui->tb4Sub2S2S2->setUpWg(ui->tb4Sub2S2S1);
    ui->tb4Sub2S2->showChildWgs(false);

    //tb4Sub3设置
    ui->tb4Sub3->addChildWg(ui->tb4Sub3S1);
    ui->tb4Sub3->showChildWgs(false);

    //tb4Sub4设置  tb4Sub4S4:背光亮度
    ui->tb4Sub4->addChildWg(ui->tb4Sub4S1);
//    ui->tb4Sub4->addChildWg(ui->tb4Sub4S3);
    ui->tb4Sub4->addChildWg(ui->tb4Sub4S4);
    ui->tb4Sub4->addChildWg(ui->tb4Sub4S5);
    ui->tb4Sub4->addChildWg(ui->tb4Sub4S6);
    ui->tb4Sub4S1->setDownWg(ui->tb4Sub4S4);
    ui->tb4Sub4S4->setDownWg(ui->tb4Sub4S5);
//    ui->tb4Sub4S4->setDownWg(ui->tb4Sub4S5);
    ui->tb4Sub4S5->setDownWg(ui->tb4Sub4S6);
    ui->tb4Sub4S6->setDownWg( ui->tb4Sub4S1);

    ui->tb4Sub4S6->setUpWg(ui->tb4Sub4S5);
    ui->tb4Sub4S5->setUpWg(ui->tb4Sub4S4);
    ui->tb4Sub4S4->setUpWg(ui->tb4Sub4S1);
//    ui->tb4Sub4S3->setUpWg(ui->tb4Sub4S1);
    ui->tb4Sub4S1->setUpWg(ui->tb4Sub4S6);
    ui->tb4Sub4->showChildWgs(false);

    //tb4Sub4S1
    ui->tb4Sub4S1->showChildWgs(false);
//    ui->tb4Sub4S3->showChildWgs(false);
    ui->tb4Sub4S1S1->hide();
    ui->tb4Sub4S1S2->hide();
    ui->tb4Sub4S3S1->hide();
    ui->tb4Sub4S3S2->hide();
    //tb4Sub4S5
    ui->tb4Sub4S5->addChildWg(ui->tb4Sub4S5S1);
    ui->tb4Sub4S5->addChildWg(ui->tb4Sub4S5S2);
    ui->tb4Sub4S5S1->setDownWg(ui->tb4Sub4S5S2);
    ui->tb4Sub4S5S2->setUpWg(ui->tb4Sub4S5S1);
    ui->tb4Sub4S5->showChildWgs(false);

    //tb4Sub5设置
    ui->tb4Sub5->addChildWg(ui->tb4Sub5S1);
    ui->tb4Sub5->addChildWg(ui->tb4Sub5S2);
    ui->tb4Sub5->addChildWg(ui->tb4Sub5S3);
    ui->tb4Sub5->addChildWg(ui->tb4Sub5S4);
    ui->tb4Sub5->addChildWg(ui->tb4Sub5S5);
    ui->tb4Sub5S1->setDownWg(ui->tb4Sub5S2);
    ui->tb4Sub5S2->setDownWg(ui->tb4Sub5S3);
    ui->tb4Sub5S3->setDownWg(ui->tb4Sub5S4);
    ui->tb4Sub5S4->setDownWg(ui->tb4Sub5S5);
    ui->tb4Sub5S5->setDownWg(ui->tb4Sub5S1);
    ui->tb4Sub5S5->setUpWg(ui->tb4Sub5S4);
    ui->tb4Sub5S4->setUpWg(ui->tb4Sub5S3);
    ui->tb4Sub5S3->setUpWg(ui->tb4Sub5S2);
    ui->tb4Sub5S2->setUpWg(ui->tb4Sub5S1);
    ui->tb4Sub5S1->setUpWg(ui->tb4Sub5S5);
    ui->tb4Sub5->showChildWgs(false);

    ui->tb4Sub5S5->addChildWg(ui->tb4Sub5S5S1);
    ui->tb4Sub5S5->addChildWg(ui->tb4Sub5S5S2);
    ui->tb4Sub5S5S1->setDownWg(ui->tb4Sub5S5S2);
    ui->tb4Sub5S5S2->setUpWg(ui->tb4Sub5S5S1);
    ui->tb4Sub5S5->showChildWgs(false);

    ui->tb4Sub5S3->addChildWg(ui->tb4Sub5S3S1);
    ui->tb4Sub5S3->showChildWgs(false);

    //设置tb5的关联按钮
    ui->tb5->setUpWg(ui->tb2);
    ui->tb5->setLeftWg(ui->tb4);
    ui->tb5->setDownWg(ui->tb2);
    ui->tb5->setRightWg(ui->tb4);
    ui->tb5->addChildWg(ui->tb5Sub1);
    ui->tb5->addChildWg(ui->tb5Sub2);
    ui->tb5Sub1->setDownWg(ui->tb5Sub2);
    ui->tb5Sub2->setDownWg(ui->tb5Sub1);
    ui->tb5Sub1->setUpWg(ui->tb5Sub2);
    ui->tb5Sub2->setUpWg(ui->tb5Sub1);

    ui->tb5Sub1->addChildWg(ui->tb5Sub1S1);
    ui->tb5Sub1->addChildWg(ui->tb5Sub1S2);
    ui->tb5Sub1->addChildWg(ui->tb5Sub1S3);
    ui->tb5Sub1->addChildWg(ui->tb5Sub1S4);
    ui->tb5Sub1->addChildWg(ui->tb5Sub1S5);
    ui->tb5Sub1->addChildWg(ui->tb5Sub1S6);
    ui->tb5Sub1->addChildWg(ui->tb5Sub1S7);
    ui->tb5Sub1->addChildWg(ui->tb5Sub1S8);

    ui->tb5Sub2->addChildWg(ui->tb5Sub2S1);
    ui->tb5Sub2->addChildWg(ui->tb5Sub2S2);
    ui->tb5Sub2S1->setUpWg(ui->tb5Sub2S2);
    ui->tb5Sub2S2->setUpWg(ui->tb5Sub2S1);
    ui->tb5Sub2S1->setDownWg(ui->tb5Sub2S2);
    ui->tb5Sub2S2->setDownWg(ui->tb5Sub2S1);

    ui->tb5Sub1S1->setDownWg(ui->tb5Sub1S2);
    ui->tb5Sub1S2->setDownWg(ui->tb5Sub1S3);
    ui->tb5Sub1S3->setDownWg(ui->tb5Sub1S4);
    ui->tb5Sub1S4->setDownWg(ui->tb5Sub1S5);
    ui->tb5Sub1S5->setDownWg(ui->tb5Sub1S6);
    ui->tb5Sub1S6->setDownWg(ui->tb5Sub1S7);
    ui->tb5Sub1S7->setDownWg(ui->tb5Sub1S8);
    ui->tb5Sub1S8->setUpWg(ui->tb5Sub1S7);
    ui->tb5Sub1S7->setUpWg(ui->tb5Sub1S6);
    ui->tb5Sub1S6->setUpWg(ui->tb5Sub1S5);
    ui->tb5Sub1S5->setUpWg(ui->tb5Sub1S4);
    ui->tb5Sub1S4->setUpWg(ui->tb5Sub1S3);
    ui->tb5Sub1S3->setUpWg(ui->tb5Sub1S2);
    ui->tb5Sub1S2->setUpWg(ui->tb5Sub1S1);


    ui->tb5Sub1->showChildWgs(false);
    ui->tb5Sub2->showChildWgs(false);

    ui->tb5->showChildWgs(false);
    ui->tb1->showChildWgs(false);
    ui->tb2->showChildWgs(false);
    ui->tb3->showChildWgs(false);
    ui->tb4->showChildWgs(false);

    ui->tb1->setIfTop(true);
    ui->tb2->setIfTop(true);
    ui->tb3->setIfTop(true);
    ui->tb4->setIfTop(true);
    ui->tb5->setIfTop(true);
    ui->tb1->setParentWg(this);
    ui->tb2->setParentWg(this);
    ui->tb3->setParentWg(this);
    ui->tb4->setParentWg(this);
    ui->tb5->setParentWg(this);


    //不赋值的就是不需进行值调整，只赋值需要的
    //赋值功能区分值,默认是NONE,非功能类菜单按钮不需赋值
    ui->tb1Sub1->setFunctionType(AUTOMODE);
    ui->tb1Sub2->setFunctionType(MANUALMODE);
    ui->tb1Sub3->setFunctionType(PAUSEMODE2);
    ui->tb1Sub4->setFunctionType(PAUSEMODE3);
    ui->tb1Sub5->setFunctionType(PAUSEMODE1);
    ui->tb1Sub6->setFunctionType(DISCHARGINGCALIB);

    ui->tb2Sub1->setFunctionType(AUTOHEAT);
    ui->tb2Sub2->setFunctionType(MANUALHEAT);

    ui->tb3Sub1->setFunctionType(QUICKMODE);
    ui->tb3Sub2->setFunctionType(PRECISEMODE);

    ui->tb4Sub1S1->setFunctionType(PUSHAMOUNT);
    ui->tb4Sub1S2->setFunctionType(PUSHSPEED);
    ui->tb4Sub1S3->setFunctionType(FACEANGLE);
    ui->tb4Sub1S4->setFunctionType(MANUALSPEED);
    ui->tb4Sub1S5->setFunctionType(PARAMSET);

    ui->tb4Sub2S1S1->setFunctionType(SINGLEPRETIME);   
    ui->tb4Sub2S1S2->setFunctionType(SINGLEPREINTENSE);
    ui->tb4Sub2S1S3->setFunctionType(SINGLETIME);      
    ui->tb4Sub2S1S4->setFunctionType(SINGLEINTENSE);   

    ui->tb4Sub2S2S1->setFunctionType(MULTIPRETIME);
    ui->tb4Sub2S2S2->setFunctionType(MULTIPREINTENSE);
    ui->tb4Sub2S2S3->setFunctionType(MULTITIME);
    ui->tb4Sub2S2S4->setFunctionType(MULTIINTENSE);

    ui->tb4Sub3S1->setFunctionType(HEATINGTIME);

    ui->tb4Sub4S1->setFunctionType(TENSIONTES);
//    ui->tb4Sub4S3->setFunctionType(VLIGHT);
    ui->tb4Sub4S4->setFunctionType(MENUALPHA);

    ui->tb4Sub5S3S1->setFunctionType(DISCHARGINGCALIB );

    ui->tb4Sub5S4->setFunctionType( SELFCHECK );
    ui->tb4Sub5S5S1->setFunctionType(STANDBY) ;
    ui->tb4Sub5S5S2->setFunctionType(POWEROFF) ;
    this->refreshLanguage();    //刷新带有进度条的button语言  lbs-2017/4/25

    ui->dateTimeEdit->setVisible(false);


    //焦点变化的消息
    ui->tb5Sub1->setDynamicID(1);
    ui->tb5Sub1S1->setDynamicID(2);
    ui->tb5Sub1S8->setDynamicID(3);
    ui->tb5Sub2->setDynamicID(4);
    ui->tb1->setDynamicID(11);
    ui->tb2->setDynamicID(12);
    ui->tb3->setDynamicID(13);
    //按键反应的消息
    ui->tb1Sub1->setDynamicID(21);
    ui->tb1Sub2->setDynamicID(22);
    ui->tb1Sub3->setDynamicID(23);
    ui->tb1Sub4->setDynamicID(24);
    ui->tb1Sub5->setDynamicID(25);
    ui->tb1Sub6->setDynamicID(26);
    ui->tb2Sub1->setDynamicID(31);
    ui->tb2Sub2->setDynamicID(32);
    ui->tb3Sub1->setDynamicID(41);
    ui->tb3Sub2->setDynamicID(42);


    ui->tb4Sub4S5S1->setDynamicID(441);  // 简体中文 lbs  重构时 可以用宏定义来表示
    ui->tb4Sub4S5S2->setDynamicID(442);  // 英文  lbs

    ui->tb4Sub5S1->setDynamicID(80);
    ui->tb4Sub5S2->setDynamicID(81);


    ui->tb4Sub5S3->setDynamicID(82);
    ui->tb4Sub5S4->setDynamicID(83);
    ui->tb4Sub5S5->setDynamicID(84);
    //ui->tb4Sub5S6->setDynamicID(85);
}

//! 带有进度条的button 语言设置，带有进度条，所以需要重绘button。 lbs 2017-4-25
void MainWindow::refreshLanguage()
{
    QSettings settings(SETTING_FILE,QSettings::IniFormat);

    ui->tb4Sub1S1->setInitial(getLanguageTr(tr_propulsion),1,100,settings.value("PushAmmount").toInt(),1);  //推进量
    ui->tb4Sub1S2->setInitial(getLanguageTr(tr_proSpeed),1,100,settings.value("PushSpeed").toInt(),1);      //推进速度
    ui->tb4Sub1S3->setInitial(getLanguageTr(tr_endAngle),5,50,settings.value("FaceAngle").toInt(),5);       //端面角度
    ui->tb4Sub1S4->setInitial(getLanguageTr(tr_manualSpeed),0,9,settings.value("ManualSpeed").toInt(),1);   //手动速度
    ui->tb4Sub1S5->setInitial(getLanguageTr(tr_dischargeValue),0,1,settings.value("ParamSel").toInt(),1);   //放电参数

    ui->tb4Sub2S1S1->setInitial(getLanguageTr(tr_preWeldTime),0,1000,settings.value("SinglePreMeltTime").toInt(),10);       //预熔时间
    ui->tb4Sub2S1S2->setInitial(getLanguageTr(tr_preWeldStrength),0,255,settings.value("SinglePreMeltIntense").toInt(),1);  //预熔强度
    ui->tb4Sub2S1S3->setInitial(getLanguageTr(tr_weldTime),1000,9900,settings.value("SingleMeltTime").toInt(),100);         //熔接时间
    ui->tb4Sub2S1S4->setInitial(getLanguageTr(tr_weldStrength),0,255,settings.value("SingleMeltIntense").toInt(),1);        //熔接强度

    ui->tb4Sub2S2S1->setInitial(getLanguageTr(tr_preWeldTime),0,1000,settings.value("MultiPreMeltTime").toInt(),10);        //预熔时间
    ui->tb4Sub2S2S2->setInitial(getLanguageTr(tr_preWeldStrength),0,255,settings.value("MultiPreMeltIntense").toInt(),1);   //预熔强度
    ui->tb4Sub2S2S3->setInitial(getLanguageTr(tr_weldTime),1000,9900,settings.value("MultiMeltTime").toInt(),100);          //熔接时间
    ui->tb4Sub2S2S4->setInitial(getLanguageTr(tr_weldStrength),0,255,settings.value("MultiMeltIntense").toInt(),1);         //熔接强度

    ui->tb4Sub3S1->setInitial(getLanguageTr(tr_heatingTime),1,45,settings.value("HeatingTime").toInt(),1);      //加热时间

    ui->tb4Sub4S4->setInitial(getLanguageTr(tr_menuTransparency),1,100,settings.value("BackLight").toInt(),1);  //菜单透明

    ui->tb4Sub5S3S1->setInitial(getLanguageTr(tr_dischargeTimes),0,9900,settings.value("DischargingTime").toInt(),100); //放电次数
    ui->tb4Sub5S5S1->setInitial(getLanguageTr(tr_standby),0,60,settings.value("StandBy").toInt(),1);     //待机(min)
    ui->tb4Sub5S5S2->setInitial(getLanguageTr(tr_shutdown),0,60,settings.value("TurnOff").toInt(),5);           //关机(min)

    ui->tb4Sub4S1->setInitial(getLanguageTr(tr_tensionTest),0,1,settings.value("TensionTest").toInt(),1);
//    ui->tb4Sub4S3->setInitial(getLanguageTr(tr_Vtype),0,1,settings.value("Vtype").toInt(),1);
}

void MainWindow::refreshDynamicUI(int id)
{
    QFile file(DEFAULT_FILE);
    if( id == 80 )   // 左工要求， 按5下可保存为出厂设置   lbs-2017-7-10
    {
        my_saveSettingCount++;
        if( my_saveSettingCount >= 5)
        {
            if(file.exists()){
                QFile::remove(DEFAULT_FILE);
            }
            QFile::copy(SETTING_FILE,DEFAULT_FILE);
        }
    }else{
        my_saveSettingCount = 0;
    }


    switch(id) {
        case 1:
            refreshRecord(0);
            break;
        case 2:
            upRefreshRecord(0);
            break;
        case 3:
            downRefreshRecord(0);
            break;
         case 4:
            refreshStatistic(0);
            break;
        case 11:
        case 21:
        case 22:
        case 23:
        case 24:
        case 25:
        case 26:
            refreshMeltMode(0);
            break;
        case 12:
        case 31:
        case 32:
            refreshHeatMode(0);
            break;
        case 13:
            break;
        case 41:
        case 42:
            refreshSpeedMode(0);
            break;        
//        case 80:
//            qDebug() << "save setting" <<__FILE__<<__LINE__;
//            if(file.exists()){
//                QFile::remove(DEFAULT_FILE);
//            }
//            QFile::copy(SETTING_FILE,DEFAULT_FILE);
//            break;
        case 81:

            if(file.exists()){
                QFile::remove(SETTING_FILE);
            }
            QFile::copy(DEFAULT_FILE,SETTING_FILE);
            //熔接记录清零
            this->dataProvider->setIniItem("TensionTest",0);
            this->dataProvider->setIniItem("Vtype",0);

             QProcess::execute("reboot");
            break;
        case 82:
            this->dataProvider->setIniItem("TensionTest",0);
            break;
        case 83:
            this->selectCheck();
            break;
        case 84:
            break;
        case 85:
            this->dataProvider->setIniItem("SuccessNum",0);
            this->dataProvider->setIniItem("FailNum",0);
            this->dataProvider->cleanRecords();
            refreshRecord(0);
            break;
        case 441:
            switchLanguage(UI_ZH);   //语言切换， 该值需自己设置  lbs-2017/4/26
            ui->tb5->setText( getLanguageTr(tr_enum_record) );
            break;
        case 442:
            switchLanguage(UI_EN);
            ui->tb5->setText( getLanguageTr(tr_enum_record) );
            break;

        default:
            break;
    }
}

void MainWindow::selectCheck()
{
    QString str;
    int x1 = this->dataProvider->getIniItemVal("SinglePreMeltTime");
    int x2 = this->dataProvider->getIniItemVal("SinglePreMeltIntense");
    int x3 = this->dataProvider->getIniItemVal("SingleMeltTime");
    int x4 = this->dataProvider->getIniItemVal("SingleMeltIntense");
    int x5 = this->dataProvider->getIniItemVal("MultiPreMeltTime");
    int x6 = this->dataProvider->getIniItemVal("MultiPreMeltIntense");
    int x7 = this->dataProvider->getIniItemVal("MultiMeltTime");
    int x8 = this->dataProvider->getIniItemVal("MultiMeltIntense");

    int x9 = this->dataProvider->getIniItemVal("MeltMode");
    int x10 = this->dataProvider->getIniItemVal("HeatingMode");
    int x11 = this->dataProvider->getIniItemVal("SpeedMode");

    float y1 = (this->dataProvider->GetFusionReg(rCORETEMP)*503.975 )/0x10000 - 273.15;
    //float y2 = (this->dataProvider->GetFusionReg(rVPIN) * 13.0)/0x10000;
    float y2 = this->dataProvider->getBattery();

    //qDebug() << x1<< x2 << x3 << x4 << x5 << x6 << x7 << x8 << x9 << x10 << x11 << y1 << y2 <<__FILE__<<__LINE__;

    if( (x1 || x2 || x3 || x4 || x5 || x6|| x7 || x8 || x9 || x10 || x11) == 0 )
    {
         my_messageBox->setMessage("提示",tr("系统参数为零，请恢复出厂设置"));
    }else
    {
        str += tr("单模预熔时间::") + QString().setNum( x1);
        str += tr("      单模预熔强度::") + QString().setNum( x2);
        str += tr("\n单模熔接时间::") + QString().setNum( x3);
        str += tr("      单模熔接强度::") + QString().setNum(x4);
        str += tr("\n多模预熔时间::") + QString().setNum(x5 );
        str += tr("      多模预熔强度::") + QString().setNum( x6);
        str += tr("\n多模熔接时间::") + QString().setNum(x7 );
        str += tr("      多模熔接强度::") + QString().setNum( x8);
        str += tr("\n熔接模式::") + QString().setNum( x9);
        str += tr("  加热模式::") + QString().setNum(x10 );
        str += tr("  速度模式::") + QString().setNum( x11);
        str += tr("\ncpu温度::") + QString().setNum( y1);
        str += tr("  电池电压::") + QString().setNum( y2);
        my_messageBox->setMessage(tr("提示:系统自检成功"), str);
    }
}


//! 语言设置初始化  lbs-2017/4/26
void MainWindow::setLanguage()
{

    QSettings settings(SETTING_FILE,QSettings::IniFormat);
    LANGUAGE languageIndex = LANGUAGE (settings.value("Language").toInt());
    translateUI();
    switchLanguage(languageIndex);
    enterStateMode();
}

void MainWindow::setTranslator(QTranslator *translator)
{
    this->my_translator = translator;
}


//!语言设置初始化  lbs-2017/4/26
void MainWindow::changeEvent(QEvent *event)
{
    switch (event->type())
    {
    case QEvent::LanguageChange:
        translateUI();
        break;
    default:
        QWidget::changeEvent(event);
    }
}

//! 查询语言设置文档l bs-2017/4/26
void MainWindow::switchLanguage(LANGUAGE language)
{
        QString language_qm;
        int language_count;
        switch(language)
        {
        case UI_ZH:
            language = UI_ZH;
            language_count = 0;
            language_qm = QString(":/language_zh");
            break;

        case UI_EN:
            language = UI_EN;
            language_count = 1;
            language_qm = QString(":/language_en");
            break;

        default:
            language = UI_ZH;
            language_count = 0;
            language_qm = QString(":/language_zh");
            break;
        }
        if(my_current_language != language)
        {
            my_current_language = language;
            my_translator->load(language_qm);
            QSettings settings(SETTING_FILE,QSettings::IniFormat);
            settings.setValue("Language",language_count);
        }

}


//! 语言转换器   程序启动之后 ，动态变化语言 lbs-2017/4/26
void MainWindow::translateUI()
{
    //界面设置
    ui->tb1->setText( getLanguageTr(tr_enum_weld) );
    ui->tb1Sub1->setText( getLanguageTr(tr_auto) );
    ui->tb1Sub2->setText( getLanguageTr(tr_manual) );
    ui->tb1Sub3->setText( getLanguageTr(tr_continuous) );
    ui->tb1Sub4->setText( getLanguageTr(tr_alignment ) );
    ui->tb1Sub5->setText( getLanguageTr(tr_suspension) );
    ui->tb1Sub6->setText( getLanguageTr(tr_openCurrent) );

    ui->tb2->setText( getLanguageTr(tr_enum_Heating) );
    ui->tb2Sub1->setText( getLanguageTr(tr_heatingAuto) );
    ui->tb2Sub2->setText( getLanguageTr(tr_heatingManu) );

    ui->tb3->setText( getLanguageTr(tr_enum_speed) );
    ui->tb3Sub1->setText( getLanguageTr(tr_fast) );
    ui->tb3Sub2->setText( getLanguageTr(tr_precision) );

    ui->tb4->setText( getLanguageTr(tr_enum_system) );
    ui->tb4Sub1->setText( getLanguageTr(tr_weld) );

    ui->tb4Sub2->setText( getLanguageTr(tr_discharge) );
    ui->tb4Sub2S1->setText( getLanguageTr(tr_singleMode) );
    ui->tb4Sub2S2->setText( getLanguageTr(tr_multimode) );

    ui->tb4Sub3->setText( getLanguageTr(tr_heatingSetting) );

    ui->tb4Sub4->setText( getLanguageTr(tr_machineSettings) );

    ui->tb4Sub4S5->setText( getLanguageTr(tr_language) );
    ui->tb4Sub4S5S1->setText( getLanguageTr(tr_chinese) );
    ui->tb4Sub4S5S2->setText( getLanguageTr(tr_english) );

    ui->tb4Sub4S6->setText( getLanguageTr(tr_timeSetting) );

    ui->tb4Sub5->setText( getLanguageTr(tr_maintenance) );
    ui->tb4Sub5S1->setText( getLanguageTr(tr_save) );
    ui->tb4Sub5S2->setText( getLanguageTr(tr_restore) );
    ui->tb4Sub5S3->setText( getLanguageTr(tr_replace) );

    ui->tb4Sub5S4->setText( getLanguageTr(tr_self_test) );
    ui->tb4Sub5S5->setText( getLanguageTr(tr_energy) );
    //ui->tb4Sub5S6->setText( getLanguageTr(tr_clear) );

    ui->tb5Sub1->setText( getLanguageTr(tr_details) );
    ui->tb5Sub2->setText( getLanguageTr(tr_statistics) );
    ui->tb5Sub2S1->setText( getLanguageTr(tr_success) );
    ui->tb5Sub2S2->setText( getLanguageTr(tr_failure) );

    //刷新带有进度条的button语言
    refreshLanguage();

    //其他设置
    QStringList t_dataProviderTrList;
    t_dataProviderTrList << getLanguageTr(tr_engineering) <<getLanguageTr(tr_saveSettings)<<
                            getLanguageTr(tr_initialization) <<getLanguageTr(tr_rapidAdvance)<<
                            getLanguageTr(tr_precisionPropulsion) <<getLanguageTr(tr_precisionAlignment)<<
                            getLanguageTr(tr_suspend) <<getLanguageTr(tr_dischargeWelding)<<
                            getLanguageTr(tr_saveTestResults) <<getLanguageTr(tr_estimatedLoss)<<
                            getLanguageTr(tr_weldFailure) <<getLanguageTr(tr_tensionTest)<<
                            getLanguageTr(tr_slotDetection) <<getLanguageTr(tr_endReport)<<getLanguageTr(tr_free);

    t_dataProviderTrList << getLanguageTr(tr_auto) <<getLanguageTr(tr_manual)<<
                            getLanguageTr(tr_continuous) <<getLanguageTr(tr_alignment )<<
                            getLanguageTr(tr_suspension) <<getLanguageTr(tr_openCurrent);

    t_dataProviderTrList << getLanguageTr(tr_fast) <<getLanguageTr(tr_precision);
    t_dataProviderTrList << getLanguageTr(tr_heatingAuto) <<getLanguageTr(tr_heatingManu);

    dataProvider->setLanguageTr(t_dataProviderTrList);


    QStringList t_displaywidgetTrList;
    t_displaywidgetTrList << getLanguageTr(tr_hardwareV) <<getLanguageTr(tr_softwareV)<<
                             getLanguageTr(tr_equipmentNumber) <<getLanguageTr(tr_dischargeNormal )<<
                             getLanguageTr(tr_dischargeTooStrong) <<getLanguageTr(tr_dischargeTooWeak)<<
                             getLanguageTr(tr_fromLeft) <<getLanguageTr(tr_fromRight)<<
                             getLanguageTr(tr_dischargeAnomaly) <<getLanguageTr(tr_positionAdjustment)<<
                             getLanguageTr(tr_intensityAdjustment) <<getLanguageTr(tr_confirm)<<
                             getLanguageTr(tr_prompt) ;

    bgWidget->setLanguageTr(t_displaywidgetTrList);

}

//! 国际化语言标记，所有语言以此为母本  lbs-2017/4/26
QString MainWindow::getLanguageTr(LanguageSetting index)
{
    QString t_string;
    switch (index){
    case tr_enum_weld:      t_string = tr("tr_enum_weld");break;
    case tr_auto:           t_string = tr("tr_auto");break;
    case tr_manual:         t_string = tr("tr_manual");break;
    case tr_continuous:     t_string = tr("tr_continuous");break;
    case tr_alignment :     t_string = tr("tr_alignment ");break;
    case tr_suspension:     t_string = tr("tr_suspension");break;
    case tr_openCurrent:    t_string = tr("tr_openCurrent");break;

    case tr_enum_Heating:   t_string = tr("tr_enum_Heating");break;
    case tr_heatingAuto:    t_string = tr("tr_heatingAuto");break;
    case tr_heatingManu:    t_string = tr("tr_heatingManu");break;

    case tr_enum_speed:     t_string = tr("tr_enum_speed");break;
    case tr_fast:           t_string = tr("tr_fast");break;
    case tr_precision:      t_string = tr("tr_precision");break;

    case tr_enum_system:    t_string = tr("tr_enum_system");break;
    case tr_weld:           t_string = tr("tr_weld");break;
    case tr_propulsion:     t_string = tr("tr_propulsion");break;
    case tr_proSpeed:       t_string = tr("tr_proSpeed");break;
    case tr_endAngle:       t_string = tr("tr_endAngle");break;
    case tr_manualSpeed:    t_string = tr("tr_manualSpeed");break;
    case tr_dischargeValue: t_string = tr("tr_dischargeValue");break;
    case tr_discharge:      t_string = tr("tr_discharge");break;
    case tr_singleMode:     t_string = tr("tr_singleMode");break;
    case tr_multimode:      t_string = tr("tr_multimode");break;
    case tr_preWeldTime:    t_string = tr("tr_preWeldTime");break;
    case tr_preWeldStrength:t_string = tr("tr_preWeldStrength");break;
    case tr_weldTime:       t_string = tr("tr_weldTime");break;
    case tr_weldStrength:   t_string = tr("tr_weldStrength");break;
    case tr_heatingSetting: t_string = tr("tr_heatingSetting");break;
    case tr_heatingTime:    t_string = tr("tr_heatingTime");break;
    case tr_machineSettings:t_string = tr("tr_machineSettings");break;
    case tr_tensionTest:    t_string = tr("tr_tensionTest");break;
    case tr_Vtype:          t_string = tr("tr_Vtype");break;
    case tr_open:           t_string = tr("tr_open");break;
    case tr_close:          t_string = tr("tr_close");break;
    case tr_menuTransparency:t_string = tr("tr_menuTransparency");break;
    case tr_language:       t_string = tr("tr_language");break;
    case tr_chinese:        t_string = tr("tr_chinese");break;
    case tr_english:        t_string = tr("tr_english");break;
    case tr_timeSetting:    t_string = tr("tr_timeSetting");break;
    case tr_maintenance:    t_string = tr("tr_maintenance");break;
    case tr_save:           t_string = tr("tr_save");break;
    case tr_restore:        t_string = tr("tr_restore");break;
    case tr_replace:        t_string = tr("tr_replace");break;
    case tr_dischargeTimes: t_string = tr("tr_dischargeTimes");break;
    case tr_self_test:      t_string = tr("tr_self_test");break;
    case tr_energy:         t_string = tr("tr_energy");break;
    case tr_standby:        t_string = tr("tr_standby");break;
    case tr_shutdown:       t_string = tr("tr_shutdown");break;
    case tr_clear:          t_string = tr("tr_clear");break;
    case tr_userGuide:      t_string = tr("tr_userGuide");break;
    case tr_operation:      t_string = tr("tr_operation");break;
    case tr_guide:          t_string = tr("tr_guide");break;
    case tr_video:          t_string = tr("tr_video");break;

    case tr_enum_record:    t_string = tr("tr_enum_record");break;
    case tr_details:        t_string = tr("tr_details");break;
    case tr_statistics:     t_string = tr("tr_statistics");break;
    case tr_success:        t_string = tr("tr_success");break;
    case tr_failure:        t_string = tr("tr_failure");break;

    //message
    case tr_MS_replace:     t_string = tr("tr_MS_replace");break;
    case tr_MS_Init:        t_string = tr("tr_MS_Init");break;
    case tr_MS_twoFiber:    t_string = tr("tr_MS_twoFiber");break;
    case tr_MS_rightFiber:  t_string = tr("tr_MS_rightFiber");break;
    case tr_MS_leftFiber:   t_string = tr("tr_MS_leftFiber");break;
    case tr_MS_leftBad:     t_string = tr("tr_MS_leftBad");break;
    case tr_MS_rightBad:    t_string = tr("tr_MS_rightBad");break;
    case tr_MS_bothBad:     t_string = tr("tr_MS_bothBad");break;
    case tr_MS_power:       t_string = tr("tr_MS_power");break;
    case tr_MS_closeSys:    t_string = tr("tr_MS_closeSys");break;
    case tr_MS_success:     t_string = tr("tr_MS_success");break;
    case tr_MS_failure:     t_string = tr("tr_MS_failure");break;


    //other
    case tr_currentState:   t_string = tr("tr_currentState");break;

    case tr_hardwareV:      t_string = tr("tr_hardwareV");break;
    case tr_softwareV:      t_string = tr("tr_softwareV");break;
    case tr_equipmentNumber:t_string = tr("tr_equipmentNumber");break;

    case tr_dischargeNormal :t_string = tr("tr_dischargeNormal ");break;
    case tr_dischargeTooStrong:t_string = tr("tr_dischargeTooStrong");break;
    case tr_dischargeTooWeak:t_string = tr("tr_dischargeTooWeak");break;
    case tr_fromLeft:       t_string = tr("tr_fromLeft");break;
    case tr_fromRight:      t_string = tr("tr_fromRight");break;
    case tr_dischargeAnomaly:t_string = tr("tr_dischargeAnomaly");break;
    case tr_positionAdjustment:t_string = tr("tr_positionAdjustment");break;
    case tr_intensityAdjustment:t_string = tr("tr_intensityAdjustment");break;
    case tr_confirm:        t_string = tr("tr_confirm");break;
    case tr_prompt:         t_string = tr("tr_prompt");break;

    case tr_engineering:    t_string = tr("tr_engineering");break;
    case tr_saveSettings:   t_string = tr("tr_saveSettings");break;
    case tr_initialization: t_string = tr("tr_initialization");break;
    case tr_rapidAdvance:   t_string = tr("tr_rapidAdvance");break;
    case tr_precisionPropulsion:t_string = tr("tr_precisionPropulsion");break;
    case tr_precisionAlignment:t_string = tr("tr_precisionAlignment");break;
    case tr_suspend:        t_string = tr("tr_suspend");break;
    case tr_dischargeWelding:t_string = tr("tr_dischargeWelding");break;
    case tr_saveTestResults:t_string = tr("tr_saveTestResults");break;
    case tr_estimatedLoss:  t_string = tr("tr_estimatedLoss:");break;
    case tr_weldFailure:    t_string = tr("tr_weldFailure");break;

    case tr_slotDetection:  t_string = tr("tr_slotDetection");break;
    case tr_endReport:      t_string = tr("tr_endReport");break;
    case tr_free:           t_string = tr("tr_free");break;
    case tr_spacing:        t_string = tr("tr_spacing");break;

    default:
        t_string = "NULL";
        break;
    }
    return t_string;
}

void MainWindow::ButtonBeep()
{
       this->dataProvider->SetFusionReg(rBEEP_TONE,113600);     //440hz beep
       this->dataProvider->SetFusionReg(rBEEP_ON  ,300*10000);  //0.3s
}

void MainWindow::displayComs()
{
    this->setStyleSheet( "background-color: rgb(0, 0, 0,0)");
}


void MainWindow::alarmSlot()
{
    if( this->hasFocus() && this->dataProvider->getMeltState() != MeltDebug)
    {
        this->bgWidget->setRemindMsg("环境温度过高，等设备冷却后再使用，\n以免减少机器的使用寿命.",true);
        this->bgWidget->setDisplayMode(DisplayRemind);
        this->bgWidget->repaint();
        my_displayAlarm = true;
    }
}

void MainWindow::sysreboot()
{
   QProcess::execute("reboot");
}


void MainWindow::powerOff(int enable)
{
    switch ( enable )
    {
    case 0:
        this->bgWidget->setRemindMsg("系统关机中....",true);
        this->bgWidget->setDisplayMode(DisplayRemind);
        this->bgWidget->repaint();
        break;
    case 1:
        this->bgWidget->setRemindMsg("电量严重不足，请及时充电，\n系统即将关机.",true);
        this->bgWidget->setDisplayMode(DisplayRemind);
        this->bgWidget->repaint();
        break;
    case 2:
        this->bgWidget->setDisplayMode(DisplayXY);
        this->bgWidget->repaint();
        this->setFocus();
        break;
    default:
        break;
    }
}

void MainWindow::BTPressEvent()
{
    my_StandbyEnable = true;
    my_StandbyCount = 0;
    my_TurnOffCount= 0;
}


void MainWindow::setPermissions(bool enable)
{

    connect( ui->tb4Sub1S1,SIGNAL(Unlock()),this, SLOT(UnlockPermissions()));

    ui->tb4Sub1S1->setPerm(true);
    ui->tb4Sub1S2->setPerm(true);
    ui->tb4Sub1S3->setPerm(true);
    ui->tb4Sub1S4->setPerm(true);
    ui->tb4Sub1S5->setPerm(true);

    connect( ui->tb4Sub2S1S1,SIGNAL(Unlock()),this, SLOT(UnlockPermissions()));
    ui->tb4Sub2S1S1->setPerm(true);
    ui->tb4Sub2S1S2->setPerm(true);
    ui->tb4Sub2S1S3->setPerm(true);
    ui->tb4Sub2S1S4->setPerm(true);

    connect( ui->tb4Sub2S2S1,SIGNAL(Unlock()),this, SLOT(UnlockPermissions()));
    ui->tb4Sub2S2S1->setPerm(true);
    ui->tb4Sub2S2S2->setPerm(true);
    ui->tb4Sub2S2S3->setPerm(true);
    ui->tb4Sub2S2S4->setPerm(true);

}

void MainWindow::UnlockPermissions()
{

    ui->tb4Sub1S1->setPerm(false);
    ui->tb4Sub1S2->setPerm(false);
    ui->tb4Sub1S3->setPerm(false);
    ui->tb4Sub1S4->setPerm(false);
    ui->tb4Sub1S5->setPerm(false);

    ui->tb4Sub2S1S1->setPerm(false);
    ui->tb4Sub2S1S2->setPerm(false);
    ui->tb4Sub2S1S3->setPerm(false);
    ui->tb4Sub2S1S4->setPerm(false);

    ui->tb4Sub2S2S1->setPerm(false);
    ui->tb4Sub2S2S2->setPerm(false);
    ui->tb4Sub2S2S3->setPerm(false);
    ui->tb4Sub2S2S4->setPerm(false);
}









